/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package singleton;

import java.util.concurrent.CountDownLatch;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Bryce.Alcock
 */
public class Test extends Thread{


    static Thread[] threads = new Thread[10];
    static boolean[] initStates = new boolean[10];
    static int[] singletonInstanceId = new int[10];
    static java.util.concurrent.CountDownLatch latch = new CountDownLatch(10);
    private int id=-1;
    public static void main(String args[]){
        for(int i=0;i<threads.length;++i){
           threads[i] = new Test();
           ((Test)threads[i]).id=i;
        }
        for(int i=0;i<threads.length;++i){
           threads[i].start();

        }

        for(int i=0;i<threads.length;++i){
            try {
                threads[i].join();
            } catch (InterruptedException ex) {
                Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
            }

        }

        System.out.println("id:    ->  state");
        for(int i=0;i<initStates.length;++i){
            System.out.println("id: "+i+"  -> "+initStates[i]+"   :  "+singletonInstanceId[i]);
        }
    }

    @Override
    public void run() {
        System.out.println(latch.getCount());
        latch.countDown();
        try {
            latch.await();
        } catch (InterruptedException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        }
        SingletonClass sc = SingletonClass.getInstance();
        initStates[id]=sc.isReadyInstance;
        singletonInstanceId[id]=sc.instanceid;

    }


}
